
import 'package:flutter/material.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/operation/rb_enum.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:smart_usb_android/smart_usb_android.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
扫描蓝牙设备
 */
class ScanUsbDeviceStatePage extends XPageListViewState{

  String customType;

  ScanUsbDeviceStatePage(this.customType);
  int currentSelectIndex=-1;

  @override
  Widget getListItemWidget(listItem, int removeUsePosition) {
    UsbDeviceDescription device=listItem as UsbDeviceDescription;

    return XWidgetUtils.getXBarForLeftRightWidget(
      margin: XWidgetUtils.getEdgeInsets(XTempData.defaultPageMarginLeft, 10, XTempData.defaultPageMarginRight, 0),
      isShowBottomDivider: false,
      leftWidget: XWidgetUtils.getLayoutExpanded(
          XWidgetUtils.getLayoutColumn(
              mainAxisAlignment:MainAxisAlignment.center,
              children: [
                XWidgetUtils.getWidgetText(device.product??'未知设备',
                    style: XStyleUtils.getTextStyle(
                        fontSize: XTempData.defaultTextFontSize,
                        color: XColorUtils.color333333
                    )
                ),
                XWidgetUtils.getWidgetText('${device.manufacturer}: ${device.serialNumber}',
                    style: XStyleUtils.getTextStyle(
                        fontSize: XTempData.defaultSmallTextFontSize,
                        color: XColorUtils.color999999
                    )
                )
              ]
          )
      ),
      rightWidget: XImageUtils.getImageWidgetWithAsset(
          currentSelectIndex==removeUsePosition?'select_xuanzhong.png':'select_weixuanzhong.png',
        color:currentSelectIndex==removeUsePosition?RBColors.reabamColor4:null,
        width: RBConfigs.selectImageWidth,
        height: RBConfigs.selectImageWidth,
      ),
      onPressed: (){

        //判断设备是否已授权
        XUsbUtils.hasPermission(device.device).then((bool value){
          if(value){
            if(currentSelectIndex!=removeUsePosition){
              //XLogUtils.printLog('---已授权');
              setState(() {
                currentSelectIndex=removeUsePosition;
              });
            }
          }else{
            toast('请重新插入usb设备确认授权申请');
          }
        }).onError((error, stackTrace){
          //实测: 重新插入usb设备,确认授权后,点击报错,则重新扫描usb设备
          XLogUtils.printLog('---实测: 重新插入usb设备,确认授权后,点击报错,则重新扫描usb设备');
          currentSelectIndex=-1;
          updateData();
        });

      },
    );

  }

  @override
  void onStart() {
    if(RBConfigs.versionType==VersionType.officialSelfServiceVersion
        || RBConfigs.versionType==VersionType.testSelfServiceVersion
    ){
      //自助收银
      setXTopBarWidget(
          height: null,
          child: RBWidgets_SelfService.getTitleBarWidget(
              this,
              title: '扫描设备',
              rightBt: '刷新',
              onTapRightBt: (){
                currentSelectIndex=-1;
                updateData();
              }
          )
      );

    }else{
      //HD h5
      setXTitle(titleText: '扫描设备',
        titleBarHeight: 50,
        isShowLeftWidget: false,
        rightWidgets:[
          XWidgetUtils.getWidgetButtonText(
              text: '刷新',
              buttonStyle: XStyleUtils.getButtonStyle(fontSize: 15,fontColor: RBColors.reabamColor4),
              onPressed: (){
                currentSelectIndex=-1;
                updateData();
              }
          )
        ],
      );
      //
      setXLeftFixSpace(width:458);
    }



    //
    setXBottomBarWidget(
        child:XWidgetUtils.getXButtonRoundedRectAngle(
            text: '确认',
            fontColor: Colors.white,
            onPressed: () {

              if(currentSelectIndex==-1){
                toast('请选择设备');
                return;
              }

              bool isSuccess=XUsbUtils.addNoteUsbDevice(widget.listData[currentSelectIndex], customType);

              if(isSuccess){
                XFlutterUtils.finishPage(context,result: 'ok');
              }else{
                toast('已存在usb设备记录,不能重复添加');
              }

            },
            backgroundColor: RBColors.reabamColor4
        ),
      alignment: Alignment.center,
      padding: XWidgetUtils.getEdgeInsets(RBConfigs.bottomBarButtomMarginLeftRight, 0, RBConfigs.bottomBarButtomMarginLeftRight, 0),
      backgroundColor: Colors.white,
    );
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {
    showLoading();
    XUsbUtils.scanUsbDevices(notifyScanFinish: (List<UsbDeviceDescription> scanResult){
      hideLoading();
      setXListData_addAllItems(scanResult);
    });
  }


  @override
  void onPaused() {
    super.onPaused();
    hideLoading();
  }


}